package com.bugs.activity;

import cn.bugs.vo.Evection;
import cn.hutool.core.collection.CollectionUtil;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;

@SpringBootTest
public class GatewayParallel {

    /**
     * 部署
     */
    @Test
    public void addTable(){
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
        Deployment demo1 = repositoryService.createDeployment()
                .addClasspathResource("bpmn/gateway-parallel.bpmn") // 添加流程图
                //.addClasspathResource("bpmn/gateway-parallel.png") // 对应的流程图片 支持 png|jpg|gif|svg
                .name("排他网关流程部署测试")
                .deploy();
        System.out.println("流程部署id===》"+demo1.getId());
        System.out.println("流程部署name===》"+demo1.getName());
    }

    /**
     * 启动实例
     */
    @Test
    public void startProcess(){
        // 获取流程引擎
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        // 获取 runtime 服务
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
        // 指定哪个模板
        String tempKey = "parallel";

        // 启动流程实例
        runtimeService.startProcessInstanceByKey(tempKey);
    }

    /**
     * 工作流的节点与状态的扭转
     * 这里有2个流程  分别对对应的任务 id 进行流程的推进
     *
     * 执行两次，将节点推送到 排他网关
     */
    @Test
    public void completTask(){
        String tempKey = "parallel";
        // 获取数据库的连接信息
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = defaultProcessEngine.getTaskService();

        List<Task> list = taskService.createTaskQuery().processDefinitionKey(tempKey).list();
        if(!CollectionUtil.isEmpty(list)){
            list.forEach(task->{
                taskService.complete(task.getId());
            });
        }

    }

    /**
     * 查询流程实例信息
     */
    @Test
    public void queryTask(){
        // 执行已部署的流程模板名称
        String tempKey = "parallel";
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = defaultProcessEngine.getTaskService();

        List<Task> list = taskService.createTaskQuery()
                .processDefinitionKey(tempKey) // 指定哪个流程图模板
                .list();
        if(!CollectionUtil.isEmpty(list)){
            list.forEach(x->{
                System.out.println("流程实例 id "+x.getProcessInstanceId());
                System.out.println("任务 id "+x.getId());
                System.out.println("任务负责人 "+x.getAssignee());
                System.out.println("任务名称 "+x.getName());
                System.out.println("===========================================");
            });
        }
    }

    /**
     * 仅执行 worker2  技术经理 的审批任务
     */
    @Test
    public void completWork2Task(){
        String tempKey = "parallel";
        String assign = "worker2";
        // 获取数据库的连接信息
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = defaultProcessEngine.getTaskService();

        List<Task> list = taskService.createTaskQuery()
                .processDefinitionKey(tempKey)
                .taskAssignee(assign)
                .list();
        if(!CollectionUtil.isEmpty(list)){
            list.forEach(task->{
                taskService.complete(task.getId());
            });
        }

    }
}
